package web.eshop.wizard;


import org.apache.wicket.extensions.wizard.Wizard;
import org.apache.wicket.extensions.wizard.WizardModel;
import org.apache.wicket.extensions.wizard.WizardStep;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.customer.ShopCustomerService;
import web.cms.CmsMainPage;

import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class UnregisteredWizard extends Wizard {
    private static Logger logger = Logger.getLogger(UnregisteredWizard.class.getName());

    @SpringBean
    private ShopCustomerService customerService;
    private String vehicleType = "";
    private String vehicleModel = "";
    private String heatingPrize = "";
    private String name = "";
    private String surname = "";
    private String email = "";
    private String phone = "";
    List<String> types, prizes;

    public UnregisteredWizard(String id) {
        super(id, false);

        types = Arrays.asList("Osobní auto", "Nákladní auto", "Karavan", "Dodávka", "Motorka:)");
        prizes = Arrays.asList("< 10 000 Kč", "10 000 - 20 000 Kč", "20 000 - 30 000 Kč", "> 30 000 Kč");

        WizardModel model = new WizardModel();
        model.add(new UnregWizardFirstStep());
        model.add(new UnregWizardSecondStep());
        init(model);
    }

    private final class UnregWizardFirstStep extends WizardStep {

        public UnregWizardFirstStep() {
            add(new ListChoice("vehicleType", new PropertyModel(UnregisteredWizard.this, "vehicleType"), types));
            add(new TextField("vehicleModel", new PropertyModel(UnregisteredWizard.this, "vehicleModel")));
            add(new ListChoice("heatingPrize", new PropertyModel(UnregisteredWizard.this, "heatingPrize"), prizes));
        }
    }

    private final class UnregWizardSecondStep extends WizardStep {

        public UnregWizardSecondStep() {
            add(new TextField("name", new PropertyModel(UnregisteredWizard.this, "name")));
            add(new TextField("surname", new PropertyModel(UnregisteredWizard.this, "surname")));
            add(new TextField("email", new PropertyModel(UnregisteredWizard.this, "email")));
            add(new TextField("phone", new PropertyModel(UnregisteredWizard.this, "phone")));
        }
    }

    private String createMailAnswer() {
        StringBuilder sb = new StringBuilder();
        sb.append("Objednavka od neregistrovaneho uzivatele:");
        sb.append("-----------------------------------------");
        sb.append("Typ vozidla: " + vehicleType);
        sb.append("Model: " + vehicleModel);
        sb.append("Ocekavana cena topeni: " + heatingPrize);
        sb.append("-----------------------------------------");
        sb.append("Kontaktni informace:");
        sb.append("Jmeno: " + name);
        sb.append("Prijmeni: " + surname);
        sb.append("E-mail: " + email);
        sb.append("Telefon: " + phone);
        sb.append("-----------------------------------------");
        sb.append("OrderSystem");
        return sb.toString();
    }

    //TODO
    @Override
    public void onFinish() {
        logger.info("Result=" + vehicleModel + " " + vehicleType + " " + heatingPrize + " " + name + " " + surname + " " + email + " " + phone);
        try {
            customerService.sendWizardOrderMail(createMailAnswer());
        } catch (Exception ex) {
            Logger.getLogger(UnregisteredWizard.class.getName()).log(Level.SEVERE, null, ex);
        }

        setResponsePage(CmsMainPage.class);
    }

    @Override
    public void onCancel() {
        setResponsePage(CmsMainPage.class);
    }

    public String getHeatingPrize() {
        return heatingPrize;
    }

    public void setHeatingPrize(String heatingPrize) {
        this.heatingPrize = heatingPrize;
    }

    public String getVehicleModel() {
        return vehicleModel;
    }

    public void setVehicleModel(String vehicleModel) {
        this.vehicleModel = vehicleModel;
    }

    public String getVehicleType() {
        return vehicleType;
    }

    public void setVehicleType(String vehicleType) {
        this.vehicleType = vehicleType;
    }
}
